$(document).ready(function () {

    var userId;

    getUserInfo();

    $("#saveEmail").click(function () {
        updateEmail();
    });

    $("#savePhone").click(function () {
        updatePhone();
    });

    function updateEmail() {
        $.ajax({
            type: "POST",
            cache: true,
            url:"http://"+urla+"/users/info/update.json",
            data:"email="+$("#inputEmail").val()+"&id="+userId,
            error: function(request) {
                console.log("error");
                console.log(request);
            },
            success: function(data) {
                console.log("success");
                console.log(data);
                getUserInfo();
                $('#changEmailModal').modal('hide');
            }
        });
    }

    function updatePhone() {
        $.ajax({
            type: "POST",
            cache: true,
            url:"http://"+urla+"/users/info/update.json",
            data:"phone="+$("#inputPhone").val()+"&id="+userId,
            error: function(request) {
                console.log("error");
                console.log(request);
            },
            success: function(data) {
                console.log("success");
                console.log(data);
                getUserInfo();
                $('#changPhoneModal').modal('hide');
            }
        });
    }

    function getUserInfo() {
        $.ajax({
            /*url: "./json/users/info.json",*/
            url: "http://"+urla+"/GetUserInfoServlet",
            type: "GET",
            dataType: 'JSON',
            success: function (result) {
                $("#username").text(result.user_nickname);
                if (result.user_email==null||result.user_email==""){
                    $("#emailBind").remove();
                }else {
                    $("#email").text(result.user_email);
                    var p = "<span id=\"emailBind\">(已绑定)</span>";
                    $("#emailPoint").append(p);
                }

                if (result.user_phone==null||result.user_phone==""){
                    $("#phoneBind").remove();
                }else {
                    console.log("phone:")
                    console.log(result.user_phone);
                    $("#phone").text(result.user_phone);
                    var p = "<span id=\"phoneBind\">(已绑定)</span>";
                    $("#phonePoint").append(p);
                }


                userId=result.user_id;
            }
        });
    }
});